#include "Texture.h"
#include <QDebug>

Texture::Texture(QImage* imagem)
{
    image = imagem;
    width = image->width();
    height = image->height();
    xMin = 0;
    yMin = 0;
    objectWidth = 0;
    objectHeight = 0;
}

void Texture::setObjectValues(int minX,int minY, float width, float height)
{
    xMin = minX;
    yMin = minY;
    objectWidth = width;
    objectHeight = height;
}

bool Texture::isSetten()
{
    return (xMin && yMin &&
            objectWidth && objectHeight);
}

QColor* Texture::color(QPoint p)
{
    float u = getU(p);
    float v = getV(p);
    return color(u,v);
}

QColor* Texture::color(float u, float v)
{
    int x = u * (width-1);
    int y = v * (height-1);

    return (new QColor(image->pixel(x,y)));
}

float Texture::getU(QPoint p)
{
    float u = ((p.x() - xMin)/(objectWidth));
    u = (u < 0)?0:u;
    u = (u > 1)?1:u;
    u = 1 - u;
    return u;
}

float Texture::getV(QPoint p)
{
    float v = ((yMin - p.y())/(objectHeight));
    v = (v < 0)?0:v;
    v = (v > 1)?1:v;
    v = 1 - v;
    return v;
}
